package com.xuhnagchen.automation.email;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;


public class EmailSendUtil {

    // mailFrom = "15686207622@163.com";
    //        password_mailFrom = "LDQIUGLWGLIKLKVZ";
    private final static String host_163 = "smtp.163.com";//163的服务器
    private final static String formName_163_7622 = "15686207622@163.com";//发送邮件的邮箱
    private final static String password_163_7622 = "LDQIUGLWGLIKLKVZ";//授权码
    private final static String replayAddress_163 = "15686207622@163.com"; //回复邮箱
    private final static String[] host = {host_163}; //163的服务器
    private final static String[] formName = {formName_163_7622};//发送邮件的邮箱
    private final static String[] password = {password_163_7622}; //授权码
    private final static String[] replayAddress = {replayAddress_163}; //回复邮箱


    public static void sendHtmlMail(EmailMailInfo info) throws Exception {
        int max = host.length, min = 1;
        int ran2 = ((int) (Math.random() * (max - min) + min))-1;
        System.out.println(ran2);
        //获取随机发送邮件
        info.setHost(host[ran2]);
        info.setFormName(formName[ran2]);
        info.setFormPassword(password[ran2]);   //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress[ran2]);

        Message message = getMessage(info);
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        Transport.send(message);
    }

    public static void sendTextMail(EmailMailInfo info) throws Exception {

        int max = host.length, min = 0;
        int ran2 = ((int) (Math.random() * (max - min) + min))-1;
        System.out.println(ran2);
        //获取随机发送邮件
        info.setHost(host[ran2]);
        info.setFormName(formName[ran2]);
        info.setFormPassword(password[ran2]);   //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress[ran2]);
        Message message = getMessage(info);
        //消息发送的内容
        message.setText(info.getContent());

        Transport.send(message);
    }

    private static Message getMessage(EmailMailInfo info) throws Exception {
        final Properties p = System.getProperties();
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getFormName());
        p.setProperty("mail.smtp.pass", info.getFormPassword());

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session session = Session.getInstance(p, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"), p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(true);
        Message message = new MimeMessage(session);
        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
        message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"), "LookFotoeCompany"));
        // 创建邮件的接收者地址，并设置到邮件消息中
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(info.getToAddress()));
        // 消息发送的时间
        message.setSentDate(new Date());


        return message;
    }

}
